package com.st.controller;

import com.st.base.BaseResult;
import com.st.po.StudentInsetPo;
import com.st.po.StudentNameListPo;
import com.st.pojo.Student;
import com.st.service.StudentService;
import com.st.vo.StudentNameListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "student")
@Tag(name="Student",description = "学生类的各种控制方法")
public class StudentController {

    @Resource
    private StudentService studentService;


    @RequestMapping(path = "Insert",method = RequestMethod.POST)
    @Operation(summary = "Insert",description = "插入学生的信息的接口")
    public BaseResult Insert (@Valid @RequestBody StudentInsetPo studentInsetPo) {
        boolean save = studentService.save(studentInsetPo);

        return BaseResult.ok("插入成功");
    }

    @RequestMapping(path = "getList",method = RequestMethod.POST)
    @Operation(summary = "getList",description = "根据学生姓名和年龄范围查询的学生信息的接口")
    public BaseResult getList(@Valid @RequestBody StudentNameListPo student){
       List<StudentNameListVo> studentList = studentService.getList(student);
       if (studentList!=null&&studentList.size()>0){
           return BaseResult.ok(studentList);
       }
       return BaseResult.error("没找到该学生");
    }

    @PreAuthorize("hasAnyRole('ROLE_admin')")
    @RequestMapping(path = "getNameList",method = RequestMethod.GET)
    @Operation(summary = "getList",description = "根据学生姓名和电话以158开头的学生信息的接口",deprecated = false)
    public BaseResult getNameList(){
        List<StudentNameListVo> listVos = studentService.getNameList();
        if (listVos!=null&&listVos.size()>0){
            return BaseResult.ok(listVos);
        }
        return BaseResult.error("没有查到以158开头的学生");
    }

    @PreAuthorize("hasRole('ROLE_user')")
    @RequestMapping(path = "getStudentList",method = RequestMethod.POST)
    @Operation(summary = "getStudentList",description = "根据学生姓名和年龄范围写sql语句查询的学生信息的接口")
    public BaseResult getStudentList(@Valid @RequestBody StudentNameListPo studentNameListPo){
        List<StudentNameListVo> studentList = studentService.getStudentList(studentNameListPo.getName(),studentNameListPo.getAge());
        if (studentList!=null&&studentList.size()>0){
            return BaseResult.ok(studentList);
        }
        return BaseResult.error("没找到该学生");
    }
}
